package com.kye.express.business.welfare_integral.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemIntegralCenterListRecordExchangeBinding;
import com.kye.model.bean.integral.IntegralRecordExchangeRecordBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 12/01/2018
 * @updated : 12/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class IntegralRecordExchangeItemViewBinder extends ItemViewBinder<IntegralRecordExchangeRecordBean, IntegralRecordExchangeItemViewBinder.ViewHolder> {

    private OnItemIntegralExchangeListener onItemIntegralExchangeListener;

    public IntegralRecordExchangeItemViewBinder(OnItemIntegralExchangeListener onItemIntegralExchangeListener) {
        this.onItemIntegralExchangeListener = onItemIntegralExchangeListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_integral_center_list_record_exchange, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull IntegralRecordExchangeRecordBean item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private IntegralRecordExchangeRecordBean item;
        private ItemIntegralCenterListRecordExchangeBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.integralItem)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemIntegralExchangeListener != null) {
                                onItemIntegralExchangeListener.onItemIntegralExchangeClicked(item);
                            }
                        }
                    });
        }

        public void bindData(IntegralRecordExchangeRecordBean item) {
            this.item = item;
            binding.setData(item);
        }
    }

    public interface OnItemIntegralExchangeListener{
        void onItemIntegralExchangeClicked(IntegralRecordExchangeRecordBean item);
    }
}
